{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "focal-polls",
   "metadata": {},
   "source": [
    "# Chinese Resident Identity Card Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "complete-giving",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bottom-equity",
   "metadata": {},
   "source": [
    "The function `clean_cn_ric()` cleans a column containing Chinese Resident Identity Card Number (RIC) strings, and standardizes them in a given format. The function `validate_cn_ric()` validates either a single RIC strings, a column of RIC strings or a DataFrame of RIC strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "lucky-croatia",
   "metadata": {},
   "source": [
    "RIC strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"360426199101010071\"\n",
    "* `standard`: RIC strings with proper whitespace in the proper places. Note that in the case of RIC, the compact format is the same as the standard one\n",
    "* `birthdate`: split the date parts from the number and return the birth date\n",
    "* `birthplace`: use the number to look up the place of birth of the person\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_cn_ric()` and `validate_cn_ric()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "brown-grant",
   "metadata": {},
   "source": [
    "### An example dataset containing RIC strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "charged-honduras",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"ric\": [\n",
    "            '360426199101010071',\n",
    "            '230306196304054513',\n",
    "            '230307196304054513', # invalid\n",
    "            '110223790813697', # not a RIC\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acute-biography",
   "metadata": {},
   "source": [
    "## 1. Default `clean_cn_ric`\n",
    "\n",
    "By default, `clean_cn_ric` will clean ric strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "curious-documentary",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_cn_ric\n",
    "clean_cn_ric(df, column = \"ric\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "administrative-state",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "african-taylor",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "atomic-zambia",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "spatial-contemporary",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_cn_ric(df, column = \"ric\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "developed-pacific",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "different-milan",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_cn_ric(df, column = \"ric\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "editorial-offer",
   "metadata": {},
   "source": [
    "### `birthdate`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "illegal-omega",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_cn_ric(df, column = \"ric\", output_format=\"birthdate\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "general-density",
   "metadata": {},
   "source": [
    "### `birthplace`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "legitimate-innocent",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_cn_ric(df, column = \"ric\", output_format=\"birthplace\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "prompt-companion",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned RIC strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "motivated-pilot",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_cn_ric(df, column=\"ric\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "concrete-customer",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "international-treatment",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "federal-house",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_cn_ric(df, \"ric\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "designing-consequence",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "mounted-preparation",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_cn_ric(df, \"ric\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "strategic-shock",
   "metadata": {},
   "source": [
    "## 4. `validate_cn_ric()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "athletic-straight",
   "metadata": {},
   "source": [
    "`validate_cn_ric()` returns `True` when the input is a valid RIC. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_cn_ric()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_cn_ric()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_cn_ric()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sealed-speaking",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_cn_ric\n",
    "print(validate_cn_ric(\"230306196304054513\"))\n",
    "print(validate_cn_ric(\"1234567\"))\n",
    "print(validate_cn_ric(\"360426199101010071\"))\n",
    "print(validate_cn_ric(\"360436199101010071\")) # change a bit and become invalid\n",
    "print(validate_cn_ric(\"hello\"))\n",
    "print(validate_cn_ric(np.nan))\n",
    "print(validate_cn_ric(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "former-whale",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "secret-abortion",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_cn_ric(df[\"ric\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "civilian-schedule",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "freelance-offering",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_cn_ric(df, column=\"ric\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reflected-stomach",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "alternate-library",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_cn_ric(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "exceptional-institution",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
